package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.WxAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author gatsby
 * @since 2022/10/14 15:17
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        //TODO:整合完shiro后获取用户id需要一下三行代码
        //Subject subject = SecurityUtils.getSubject();
        //MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        //Integer id = primaryPrincipal.getId();
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser)subject.getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();


        //这里先写死
        //Integer id = 1;
        CommonData<MarketAddress> commonData = wxAddressService.queryAllAddressByUserId(id);
        return BaseRespVo.ok(commonData, "成功");
    }


    /**
     * 获取地址详情的业务逻辑
     *
     * @param id
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/14 16:05
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        MarketAddress marketAddress = wxAddressService.queryAddressById(id);
        return BaseRespVo.ok(marketAddress, "成功");

    }

    /**
     * 修改地址的业务逻辑
     *
     * @param marketAddress
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/14 16:06
     */
    @RequestMapping("save")
    public BaseRespVo save(@RequestBody MarketAddress marketAddress) {
       int data= wxAddressService.saveAddress(marketAddress);

        return BaseRespVo.ok(data, "成功");
    }


    /**
     * 删除地址的逻辑
     *
     * @param id
     * @return com.cskaoyan.bean.common.BaseRespVo
     * @author gatsby
     * @since 2022/10/14 16:12
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        Integer id = (Integer)map.get("id");
        wxAddressService.delete(id);
        return BaseRespVo.ok("成功");

    }
}
